package com.kikt.animationdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.view.ViewHelper;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.start1)
    Button mStart1;
    @Bind(R.id.start2)
    Button mStart2;
    @Bind(R.id.start3)
    Button mStart3;
    protected ValueAnimator animator;
    @Bind(R.id.like)
    ImageButton mLike;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        mStart1.setOnClickListener(this);
        mStart2.setOnClickListener(this);
        mStart3.setOnClickListener(this);
        mLike.setOnClickListener(this);
    }

    private void continueAnimation() {
        animator.setFloatValues(90, 0);
        mStart2.setText("变了");
        animator.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (animator != null) {
            animator.removeAllUpdateListeners();
            animator.removeAllListeners();
            animator.cancel();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start1:
                bt1();
                break;
            case R.id.start2:
                bt2();
                break;
            case R.id.start3:
            case R.id.like:
                bt3();
                break;
        }

    }

    private void bt3() {
        int duration = 400;
        int start = 0;
        final int end = 90;
        final int end2 = 180;
        final int delay = 300;

        animator = AnimatorUtils.getAnimator(duration, start, end, end2, delay, new AnimatorUtils.OnUpdateListener() {
            @Override
            public void onUpdate(float value) {
                ViewHelper.setRotationY(mLike, value);
            }

            @Override
            public void onFirstEnd() {
                mLike.setSelected(!mLike.isSelected());
            }

            @Override
            public void onSecondStart() {
            }

            @Override
            public void onFirstStart() {
                mLike.setEnabled(false);
            }

            @Override
            public void onSecondEnd() {
                mLike.setEnabled(true);
            }
        });

        animator.start();
    }

    private void bt2() {
        animator = new ValueAnimator().setDuration(400);
        animator.setFloatValues(0, 90);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ViewHelper.setRotationY(mLike, (Float) animation.getAnimatedValue());
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mLike.setSelected(!mLike.isSelected());

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        animator.setFloatValues(90, 180);
                        animator.start();
                    }
                }, 300);
                animator.removeListener(this);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        animator.start();
    }

    private void bt1() {
        animator = new ValueAnimator().setDuration(800);
        animator.setFloatValues(0, 90);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animatedValue = (float) animation.getAnimatedValue();
                ViewHelper.setRotationY(mStart2, animatedValue);
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                MyHandler handler = new MyHandler(SecondActivity.this);
                handler.sendEmptyMessageDelayed(0, 400);
                animator.removeListener(this);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        animator.start();
    }

    static class MyHandler extends Handler {
        private SecondActivity activity;

        MyHandler(SecondActivity activity) {
            this.activity = activity;
        }

        @Override
        public void handleMessage(Message msg) {
            activity.continueAnimation();
        }
    }
}
